package com.kmproject.haifu.mvp.presenter;

import android.app.Activity;
import android.app.Application;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.widget.CustomPopupWindow;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.hfinterface.HouseReasonRemovalCallBack;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.HouseRentShowInfoContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowInfoBean;
import com.kmproject.haifu.mvp.ui.adapter.PopuwindowReasonRemovalAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/05/2019 15:58
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class HouseRentShowInfoPresenter extends BasePresenter<HouseRentShowInfoContract.Model, HouseRentShowInfoContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public HouseRentShowInfoPresenter(HouseRentShowInfoContract.Model model, HouseRentShowInfoContract.View rootView) {
        super(model, rootView);
    }
    /**
     * 下架提示
     */
    public  void houseReasonRemoval(Activity activity, View mParentView, List<HousePayWayBean> housePayWayBeans, HouseReasonRemovalCallBack houseReasonRemovalCallBack) {
        CustomPopupWindow customPopupWindow = new CustomPopupWindow.Builder().parentView(mParentView).contentView(CustomPopupWindow.inflateView(activity, R.layout.popuwindow_reason_removal))
                .animationStyle(R.style.mypopwindow_anim_style).customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {

                    }
                }).isWrap(true).isHaveBackGround(true).isShowBottom(true).build();
        View view1 = customPopupWindow.getContentView();
        RecyclerView popuwindow_reason_removal_RecyclerView = view1.findViewById(R.id.popuwindow_reason_removal_RecyclerView);
        TextView popuwindow_reason_removal__cancel_txt = view1.findViewById(R.id.popuwindow_reason_removal__cancel_txt);
        popuwindow_reason_removal_RecyclerView.setLayoutManager(new LinearLayoutManager(activity));
        PopuwindowReasonRemovalAdapter adapter=new PopuwindowReasonRemovalAdapter(housePayWayBeans);
        popuwindow_reason_removal_RecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                houseReasonRemovalCallBack.callBack(housePayWayBeans.get(position));
                customPopupWindow.dismiss();
            }
        });

        popuwindow_reason_removal__cancel_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customPopupWindow.dismiss();
            }
        });
        customPopupWindow.show();
    }
    /**
     * 获取店铺选择类型
     */
    public void dictList(String value,String category) {
        mModel.dictList(value, category).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.dictListSuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }
    /**
     * 下架房源
     * @param rentId
     * @param lowerShelfReason
     */
    public void  lowerShelfHouseRent(String rentId, String lowerShelfReason) {
        mModel.lowerShelfHouseRent(rentId,lowerShelfReason).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean))
                        {
                            mRootView.showMessage("下架成功");
                            mRootView.lowerShelfHouseRentSuccess();
                        }else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }
    /**
     * 删除一条租房信息
     * @param id
     */
    public void del(String id)
    {
        List<String> temIds=new ArrayList<>();
        temIds.add(id);
        mModel.delRentHouse(Api.APP_DOMAIN+"/app/removeHouseRent/",new Gson().toJson(temIds).toString()).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean))
                        {
                            mRootView.delRentHouseSuccess();
                        }else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    //添加收藏
     public void addGz(String id)
     {
        mModel.add(Api.APP_DOMAIN+"/app/add/houseRent/"+id).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                       if (AppUtil.isSuccess(resultBean))
                       {
                          mRootView.addGzSuccess();
                       }else {
                           mRootView.showMessage(resultBean.getMessage());
                       }
                    }
                });
     }

     //取消收藏
    public void canlGz(String id)
    {
        mModel.canl(Api.APP_DOMAIN+"/app/cancel/houseRent/"+id).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean))
                        {
                            mRootView.canlSuccess();
                        }else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }
    /**
     * 获取不同的标签显示
     * @param type
     * @param detailsBeans
     * @return
     */
    public List<HouseRentListShowBean.DetailsBean> getDetailsBeanByte(String type,List<HouseRentListShowBean.DetailsBean> detailsBeans)
    {
              if (detailsBeans==null|detailsBeans.isEmpty())
              {
                  return null;
              }
          List<HouseRentListShowBean.DetailsBean> temdetailsBeans=new ArrayList<>();
              for (HouseRentListShowBean.DetailsBean detailsBean:detailsBeans)
              {
                if (type.equals(detailsBean.getCategory()))
                {
                    temdetailsBeans.add(detailsBean);
                }
              }
              return temdetailsBeans;
    }


    /**
     * 获取详情根据id
     * @param houseId
     */
    public void viewHouseRent(String houseId,String lgt,String lat)
    {
        mModel.viewHouseRent(Api.APP_DOMAIN+"/app/viewHouseRent/"+houseId+"?lgt="+lgt+"&lat="+lat).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<HouseRentListShowInfoBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<HouseRentListShowInfoBean> houseRentListShowInfoBeanResultBean) {
                      if (AppUtil.isSuccess(houseRentListShowInfoBeanResultBean))
                      {
                          mRootView.viewHouseRentSuccess(houseRentListShowInfoBeanResultBean.getData());
                      }else {
                          mRootView.showMessage(houseRentListShowInfoBeanResultBean.getMessage());
                      }
                    }
                });
    }
    /**
     * 得到名字 字符串
     *
     * @param housePayWayBeans
     * @return
     */
    public List<String> getHousePayWay(List<HousePayWayBean> housePayWayBeans) {
        List<String> temName = new ArrayList<>();
        if (housePayWayBeans == null || housePayWayBeans.isEmpty()) {
            return temName;
        }
        for (HousePayWayBean housePayWayBean : housePayWayBeans) {
            temName.add(housePayWayBean.getName());
        }
        return temName;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
